package com.system.manager.modules.projectLibrary.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.system.manager.common.annotation.CurrSubject;
import com.system.manager.common.annotation.PreAuthorize;
import com.system.manager.common.entity.Result;
import com.system.manager.common.exception.SysException;
import com.system.manager.modules.authorized.entity.Subject;
import com.system.manager.modules.authorized.enums.SubjectType;
import com.system.manager.modules.projectLibrary.dto.ProjectDto;
import com.system.manager.modules.projectLibrary.entity.Project;
import com.system.manager.modules.projectLibrary.service.IProjectService;
import com.system.manager.modules.projectLibrary.vo.ProjectByDepartmentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jisf
 * @since 2021-11-03
 */
@Api(tags = "项目API")
@RestController
@RequestMapping("/projectLibrary/project")
public class ProjectController {

    @Autowired
    private IProjectService iProjectService;

    @ApiOperation("插入项目信息")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @PostMapping("/insert/project")
    public Result insertProject(@RequestBody Project project, @CurrSubject Subject subject) {
        try {
            iProjectService.insertProject(project,subject);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation("修改项目信息")
    @PreAuthorize({SubjectType.ENTERPRISE_ADMIN, SubjectType.PROJECT_LEADER})
    @PostMapping("/update/project")
    public Result updateProject(@RequestBody Project project, @CurrSubject Subject subject) {
        try {
            System.out.println("-------------------");
            System.out.println(project);
            System.out.println("------------------");
            iProjectService.updateProject(project, subject);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation("管理项目负责人")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @PostMapping("manage/project")
    public Result manageProjectLeader(@RequestBody Project project, @CurrSubject Subject subject) {
        try {
            iProjectService.manageProjectLeader(project);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation("项目备案审核")
    @PreAuthorize(SubjectType.zx)
    @GetMapping("/audit/project")
    public Result auditProject(@RequestParam Integer id, @RequestParam Integer putOnRecords) {
        try {
            String s = iProjectService.auditProject(id, putOnRecords);
            return Result.ok(s);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation("处理备案申请")
    @PreAuthorize(SubjectType.zx)
    @GetMapping("/apply/auditProject")
    public Result applyAuditProject(@RequestParam Integer id, @RequestParam Integer putOnRecords) {
        try {
            iProjectService.auditProject(id, putOnRecords);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation("按项目类型查询本公司所有项目")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/select/project/by/type")
    public Result selectProjectByType(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                      @RequestParam(name = "limit", required = false, defaultValue = "10") Integer size,
                                      @RequestParam(name = "projectType", required = false) String projectType,
                                      @RequestParam(name = "name", required = false) String name,
                                      @RequestParam(name = "startTime", required = false) String startTime,
                                      @RequestParam(name = "endTime", required = false) String endTime,
                                      @RequestParam(name = "department", required = false) Integer department,
                                      @RequestParam(name = "putOnRecords", required = false) String putOnRecords,
                                      @RequestParam(name = "checkAccept", required = false) String checkAccept,
                                      @CurrSubject Subject subject) {
        try {
            Result result = iProjectService.selectProjectByType(page, size, projectType, name, startTime, endTime, department, putOnRecords, checkAccept, subject);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation("备案申请")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/apply/putOnRecords")
    public Result applyPutOnRecords(@RequestParam Integer id) {
        try {
            String applyResult = iProjectService.applyPutOnRecords(id);
            return Result.ok(applyResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation("删除项目")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/delete/project")
    public Result deleteProject(@RequestParam String id) {
        try {
            String deleteResult = iProjectService.deleteProject(id);
            return Result.ok(deleteResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }


    @ApiOperation("生成项目的PDF文件")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/create/pdf")
    public Result createPdf(@RequestParam("id") String id) {

        String pdfUrl = iProjectService.createPdf(id);

        return Result.ok(pdfUrl);
    }
    
    @ApiOperation("查询有人管理的项目")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/select/project/have/projectLeader")
    public Result selectProjectHaveProjectLeader(@CurrSubject Subject subject){
        try {
            List<Project> projects = iProjectService.selectProjectHaveProjectLeader(subject);
            return Result.ok(projects);
        }catch (SysException e){
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("按部门查询公司所有项目")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/select/project/by/department")
    public Result selectProjectByDepartment(@CurrSubject Subject subject){
        try {
            List<ProjectByDepartmentVo> projectByDepartmentVos = iProjectService.selectProjectByDepartment(subject);
            return Result.ok(projectByDepartmentVos);
        }catch (SysException e){
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("设置项目负责人")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/set/projectLeader")
    public Result setProjectLeader(@RequestParam Integer id,  @RequestParam String project, @CurrSubject Subject subject){
        try {
            String s = iProjectService.setProjectLeader(id, project, subject);
            return Result.ok(s);
        }catch (SysException e){
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("分配项目")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/set/project")
    public Result setProject(@RequestParam Integer id, @RequestParam Integer project){
        try {
            String s = iProjectService.setProject(id,project);
            return Result.ok(s);
        }catch (SysException e){
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("查询自己负责的项目")
    @PreAuthorize(SubjectType.PROJECT_LEADER)
    @GetMapping("/select/one/project")
    public Result selectOneProject(@CurrSubject Subject subject){
        try {
            ProjectDto projectDto = iProjectService.selectOneProject(subject);
            return Result.ok(projectDto);
        }catch (SysException e){
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("查询所有申请项目")
    @PreAuthorize(SubjectType.zx)
    @GetMapping("/select/all/project")
    public Result selectAllProject(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                   @RequestParam(name = "limit", required = false, defaultValue = "10") Integer size,
                                   @RequestParam(name = "firm", required = false) Integer firm,
                                   @RequestParam(name = "putOnRecords", required = false)String putOnRecords,
                                   @RequestParam(name = "projectType", required = false)String projectType,
                                   @RequestParam(name = "checkAccept", required = false) String checkAccept){
        try {
            Result result = iProjectService.selectAllProject(page, size, firm, putOnRecords, projectType,checkAccept);
            return result;
        }catch (SysException e){
            e.printStackTrace();
            return Result.error(e);
        }


    }

}
